<?php
namespace app\admin\controller;

use think\Db;
use app\admin\model\Member as user;
use think\Request;
use think\Validate;

class Library extends Main
{
    private $db;

    public function  __construct() {
        parent::__construct();
        $this->db = Db::name('label_library_info');
    }

    public function index()
    {
        $this->assign('item',['item1'=>'网站管理','item2'=>'标识库列表']);
        return  $this->fetch();
    }

    public function get_table_data()
    {
        //获取分页page和limit参数
        $page=input("get.page")?input("get.page"):1;
        $page=intval($page);
        $limit=input("get.limit")?input("get.limit"):10;
        $limit=intval($limit);
        $start=$limit*($page-1);

        $count = $this->db->count("id");
        $data_list = $this->db->limit($start,$limit)->order('id desc')->select();
        $list["msg"]="";
        $list["code"]=0; //状态码
        $list["count"]=$count;
        $list["data"]=$data_list;
        if(empty($data_list)){
            $list["msg"]="暂无数据";
        }
        return json($list);
    }

    public function add_library()
   {
    if(Request::instance()->isPost()){//is post
        $post = $this->request->post();
        $validate = validate('Library');
        $res = $validate->check($post);
        if($res !== true){//没有通过验证
           $this->error($validate->getError());
        }else{//通过验证
            $data = [
					'name'=>$post['name'],
					'label_library_type_id' =>$post['label_library_type_id'],
					'label_library_description' => $post['label_library_description']
                ];
            $this->db->insert($data);
            setLog('增加标识库','Library/add_library');
            $this->success('success');
        }
    }else{
        //标识类别基础表
        $label_library_info = Db::name('label_library_type_info')->select();
        $this->assign('label_library_info',$label_library_info);
        return  $this->fetch();
    }
   }


   public function edit_library()
   {
       $id = $this->request->get('id');
       if($id){
            $res = $this->db->where('id',$id)->find();
            //标识类别基础表
			$label_library_info = Db::name('label_library_type_info')->select();
			$this->assign('label_library_info',$label_library_info);
            $this->assign('data',$res);
            return  $this->fetch();
       }else{
           $this->error('非法操作');
       }
   }

   public function deleteRow()
   {
       $id = $this->request->get('id');
       if($id){
           $res = $this->db->where('id',$id)->delete();
           if($res){
               setLog('删除标识库','Library/deleteRow');
            $this->success('success');
           }else{
               $this->error('删除失败');
           }
       }else{
           $this->error('非法操作');
       }  
   }

   public function bachDel(){
       if(Request::instance()->isPost()){//is post
           $post = $this->request->post();
           $id = implode(',',$post['id']);
           $this->db->whereIn('id',$id)->delete();
           $param = ['name'=>'批量删除标识库','way'=>'Library/bachDel','username'=>session('username'),'create_time'=>date('Y-m-d H:i:s',time())];
           \think\Hook::listen('log',$param);
           $this->success('success');
       }else{
           $this->error('非法操作');
       }
   }

   public function upload()
   {
       $file = request()->file('files');
	   if(empty($file)){
		   echo json_encode(array("code"=>999,"msg"=>"请上传文件"));
	   }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'Library');
        if($info){
        $name =  $info->getSaveName();
        $name =  str_replace('\\' ,'/', $name);
        $res = array("code"=>0,"msg"=>"","data"=>array('src'=>'Library/' . $name));
        echo json_encode($res);
        }else{
        $res = array("code"=>1,"msg"=>"","data"=>[]);
        // 上传失败获取错误信息
        echo $file->getError();
        } 
   }
   
   public function update_library()
   {
    if(Request::instance()->isPost()){//is post
            $post = $this->request->post();
            $res = $this->validate($post,'Library.edit');
            if($res !== true){//没有通过验证
               $this->error($res);
            }else{//通过验证
                $data = [
                    'name'=>$post['name'],
					'label_library_type_id' =>$post['label_library_type_id'],
					'label_library_description' => $post['label_library_description']
                ];
                $this->db->where(['id' => $post['id']])->update($data);
                setLog('修改标识库','Library/add_icon');
                $this->success('success');
            }
       }else{
           $this->error('非法操作');
       }
   }
}